package br.ufrj.neg.evento.servlet;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.allcolor.yahp.converter.CYaHPConverter;
import org.allcolor.yahp.converter.IHtmlToPdfTransformer;
import org.allcolor.yahp.converter.IHtmlToPdfTransformer.CConvertException;
import org.apache.velocity.Template;
import org.apache.velocity.tools.view.VelocityViewServlet;

import br.ufrj.neg.bean.Idioma;
import br.ufrj.neg.evento.bean.EventoBean;
import br.ufrj.neg.evento.bean.LogomarcaBean;
import br.ufrj.neg.evento.model.EventoModel;
import br.ufrj.neg.evento.model.LogomarcaModel;
import br.ufrj.neg.preparacao.bean.NegociacaoBean;
import br.ufrj.neg.preparacao.model.NegociacaoModel;
import br.ufrj.neg.servlet.PageContext;
import br.ufrj.neg.usuario.bean.UsuarioBean;
import br.ufrj.neg.util.AccessSystemResource;

public class GerarAtaReuniaoAction extends VelocityViewServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException{
		doPost(request,response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException{
		Integer idEvento = Integer.valueOf( request.getParameter("eventoId") );		
		Idioma idioma = (Idioma) request.getAttribute("idioma");
		
		HttpSession session = request.getSession(); 
		UsuarioBean ub = (UsuarioBean) session.getAttribute("usuarioBean");
		
		EventoBean eb = EventoModel.getEvento(idEvento, idioma);
		NegociacaoBean nb = new NegociacaoModel().getNegociacao( eb.getIdNegociacao() );
		HashMap<Integer,LogomarcaBean> logosMap = LogomarcaModel.getLogomarcasPorNegociacao(nb.getIdNegociacao());
		
		List< LogomarcaBean> logos =  new ArrayList<LogomarcaBean>();
		
		logos.add(logosMap.get(0));
		logos.add(logosMap.get(1));
		// endereço das imagens é ${pageContext.general.APP_PATH}imagens/logos/logoCliente.getUrl() por exemplo;
		
		AccessSystemResource asr = new AccessSystemResource();

		Template template = getTemplate("templates/systemAccess/relatorioReuniao.vm");
		String html = asr.getRelatorioReuniao(eb,nb,idioma,new PageContext(request),ub,logos,template);

		String baseURL = request.getRequestURL().toString();
		String basePath = this.getServletContext().getRealPath("/");
		
		baseURL = baseURL.substring(0,baseURL.indexOf("reuniao/") + 5 );

		ByteArrayOutputStream baos = new ByteArrayOutputStream();	


		CYaHPConverter converter = new CYaHPConverter();
		
		// contains configuration properties
		Map<String,String> properties = new HashMap<String,String>();
		List<IHtmlToPdfTransformer.CHeaderFooter> headerFooterList = new ArrayList<IHtmlToPdfTransformer.CHeaderFooter>();
		
		properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS,
				IHtmlToPdfTransformer.DEFAULT_PDF_RENDERER);
		
		properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, basePath + "css" + File.separator+ "fonts" + File.separator+ "Eurosti.ttf");
		
		headerFooterList.add(new IHtmlToPdfTransformer.CHeaderFooter(
				"<div id='divRodape'>" +
				"	<hr></hr> " +
				"	<table style='font-size:12px;width:100%;'> " +
				"		<tr> " +
				"			<td style='width:50%'>" + nb.getCliente() + "</td> " +
				"			<td style='width:50%;text-align:right;'>Página <pagenumber> de <pagecount></td> " +
				"		</tr> " +
				"	</table> "+
				"</div>",IHtmlToPdfTransformer.CHeaderFooter.FOOTER));
		
		
		try {		
			converter.convertToPdf(html,
				IHtmlToPdfTransformer.A4P, headerFooterList,baseURL, baos,
				properties);
		} catch (CConvertException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
		try {
			response.setContentType("application/pdf");
			response.setContentLength(baos.size());
			ServletOutputStream ouputStream = response.getOutputStream();
			ouputStream.write(baos.toByteArray(), 0, baos.size());
			ouputStream.flush();
			ouputStream.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
